package svc

import (
	"django_blog_gin/internal/config"
	"django_blog_gin/internal/model"
	"django_blog_gin/internal/ws"
	"fmt"

	"github.com/elastic/go-elasticsearch/v8"
	"github.com/go-playground/validator/v10"
	"github.com/jmoiron/sqlx"
)

type ServiceContext struct {
	MysqlDb   *sqlx.DB
	Config    config.Config
	Validator validator.Validate
	// PgDb      *sqlx.DB
	EsClient  *elasticsearch.Client
	WSManager *ws.Manager
}

func NewServiceContext() *ServiceContext {
	MysqlDbConn := model.ConnectMysqlDb(config.GlobalConfig.Mysql.GenerateDns())
	EsClient := model.ConnectESClient(config.GlobalConfig.Es.Addresses, config.GlobalConfig.Es.Username, config.GlobalConfig.Es.Password)
	manager := ws.NewManager()
	go manager.Start()
	fmt.Println("初始化svc成功")
	return &ServiceContext{
		MysqlDb:   MysqlDbConn,
		Config:    *config.GlobalConfig,
		Validator: *validator.New(),
		EsClient:  EsClient,
		WSManager: manager,
	}
}
